package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.Logger;

import net.smartam.leeloo.as.issuer.MD5Generator;
import net.smartam.leeloo.as.issuer.OAuthIssuer;
import net.smartam.leeloo.as.issuer.OAuthIssuerImpl;
import net.smartam.leeloo.as.request.OAuthTokenRequest;
import net.smartam.leeloo.as.response.OAuthASResponse;
import net.smartam.leeloo.common.exception.OAuthProblemException;
import net.smartam.leeloo.common.message.OAuthResponse;
import constant.Constant;

/**
 * Servlet implementation class AccessServlet
 */
@WebServlet(Constant.DEFAULT_ACCESSTOKEN)
public class AccessServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AccessServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Logger.Log("Access Server...");

		OAuthTokenRequest oauthRequest = null;

		OAuthIssuer oauthIssuer = new OAuthIssuerImpl(new MD5Generator());

		try {

			oauthRequest = new OAuthTokenRequest(request);
			Logger.Log("clientId " + oauthRequest.getClientId());
			Logger.Log("clientSecret " + oauthRequest.getClientSecret());
			Logger.Log("authzCode " + oauthRequest.getCode());
			Logger.Log("accessToken " + oauthIssuer.accessToken());
			Logger.Log("refreshToken " + oauthIssuer.refreshToken());
			// some code

			String accessToken = oauthIssuer.accessToken();
			// System.out.println("accessToken "+accessToken);
			String refreshToken = oauthIssuer.refreshToken();
			// System.out.println("refreshToken "+refreshToken);
			// some code

			OAuthResponse r = OAuthASResponse
					.tokenResponse(HttpServletResponse.SC_OK)
					.setAccessToken(accessToken).setExpiresIn("3600")
					.setRefreshToken(refreshToken).buildBodyMessage();

			response.setStatus(r.getResponseStatus());

			Logger.Log("ResponseStatus " + r.getResponseStatus());

			PrintWriter pw = response.getWriter();
			pw.print(r.getBody());
			// System.out.println("body "+r.getBody());
			pw.flush();
			pw.close();

			// if something goes wrong
		} catch (OAuthProblemException ex) {
			try {
				OAuthResponse r = OAuthResponse.errorResponse(401).error(ex)
						.buildJSONMessage();

				response.setStatus(r.getResponseStatus());

				PrintWriter pw = response.getWriter();
				pw.print(r.getBody());
				pw.flush();
				pw.close();

				response.sendError(401);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
